package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 * This is the AdminPage. The page where admins go after they login
 * Admins can see information that is specific to them.
 * 
 */
public class AdminPage extends JPanel {
	private static final long serialVersionUID = -2828828240921371142L;
	JPanel lowerPanel, adminPanel, titlePanel, create_adminPanel;

	/* titlePanel Components */
	JLabel title;

	/* adminPanel Components */
	JLabel adminName, adminPassword, adminPasswordV;
	JTextField adminNameField, adminPasswordField, adminPasswordVField;
	JButton logoutButton, create_adminButton, deleteButton;

	/* searchPanel Components */
	SearchPanel3 searchPanel;

	// Parent Panel
	GUI guiObject;
	
	public void clearFields()
	{
		adminNameField.setText("");
		adminPasswordField.setText("");
		adminPasswordVField.setText("");
	}

	public AdminPage(GUI guiObject) {
		setLayout(new GridBagLayout());

		this.guiObject = guiObject;

		createTitlePanel();
		createLowerPanel();
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(0,0,0,0),0,0);

		add(titlePanel, Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.SOUTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);

		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.NONE, new Insets(5,0,0,5),0,0);

		add(lowerPanel, Constraints.gbc);
		
		return;
	}

	private void createTitlePanel() {
		titlePanel = new JPanel(new GridBagLayout());

		title = new JLabel("WELCOME ADMIN");
		title.setForeground(Color.white);
		titlePanel.setBackground(Color.red);
		title.setFont(new Font("Serif", Font.BOLD, 48));

		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		titlePanel.add(title, Constraints.gbc);
	}

	private void createLowerPanel() {
		lowerPanel = new JPanel(new GridBagLayout());
		lowerPanel.setVisible(true);

		createAdminPanel();
		searchPanel = new SearchPanel3(guiObject, this);

		Constraints.setConstraints(0,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(searchPanel, Constraints.gbc);
		
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.BOTH,new Insets(5,5,5,5),0,0);

		lowerPanel.add(new JSeparator(SwingConstants.VERTICAL), Constraints.gbc);

		Constraints.setConstraints(2,0,1,1,0,0,GridBagConstraints.NORTHEAST,
				GridBagConstraints.NONE,new Insets(0,0,0,0),0,0);

		lowerPanel.add(adminPanel, Constraints.gbc);
	}

	private void createAdminPanel() {

		adminPanel = new JPanel(new GridBagLayout());
		create_adminPanel = new JPanel(new GridBagLayout());

		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.WEST,
				GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0);

		JPanel buttonPanel = new JPanel(new GridBagLayout());

		/*--ADDING CREATE ADMIN PANEL--*/
		
		//Making a border then making a title around it.
		Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		TitledBorder title = BorderFactory.createTitledBorder(loweredetched, "Create Admin");
		title.setTitleJustification(TitledBorder.RIGHT);
		create_adminPanel.setBorder(title);

		//Name and text field for text input for Login
		adminName = new JLabel("Name", SwingConstants.RIGHT);
		adminNameField = new JTextField();
		adminNameField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
					GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		create_adminPanel.add(adminName, Constraints.gbc);
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		create_adminPanel.add(adminNameField, Constraints.gbc);
		
		//password fields
		adminPassword = new JLabel("Password", JLabel.RIGHT);
		adminPasswordField = new JPasswordField();
		adminPasswordField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		create_adminPanel.add(adminPassword, Constraints.gbc);
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		create_adminPanel.add(adminPasswordField, Constraints.gbc);

		//Password verification and text field for text input for Login
		adminPasswordV = new JLabel("Verify PW", SwingConstants.RIGHT);
		adminPasswordVField = new JPasswordField();
		adminPasswordVField.setPreferredSize(new Dimension(200, 20));
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,0,0,5),0,0);
		create_adminPanel.add(adminPasswordV, Constraints.gbc);
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.CENTER,
				GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
		create_adminPanel.add(adminPasswordVField, Constraints.gbc);
		
		/*--ADDING CREATE BUTTON--*/
		
		create_adminButton = new JButton("Create Admin");
		create_adminButton.setActionCommand("admincreate");
		create_adminButton.addActionListener(guiObject);
		Constraints.setConstraints(1,6,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,3),0,0);
		create_adminPanel.add(create_adminButton, Constraints.gbc);
		
		/*--ADDING nameRegisterPanel TO userPanel--*/
		Constraints.setConstraints(0,3,2,2,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(0,50,0,0),0,0);
		adminPanel.add(create_adminPanel, Constraints.gbc);

		/*--ADDING DELETE BUTTON--*/
/*
		deleteButton = new JButton("Delete");
		deleteButton.setActionCommand("delete");
		deleteButton.addActionListener(guiObject);
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,120),0,0);
		
		buttonPanel.add(deleteButton, Constraints.gbc);
*/
		/*--ADDING LOGOUT BUTTON--*/

		logoutButton = new JButton("Logout");
		logoutButton.setActionCommand("logout");
		logoutButton.addActionListener(guiObject);
		
		Constraints.setConstraints(1,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,0,0),0,0);
		
		buttonPanel.add(logoutButton, Constraints.gbc);
		
		Constraints.setConstraints(0,0,1,1,1,1,GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5,0,5,3),0,0);
		
		adminPanel.add(buttonPanel, Constraints.gbc);
	}
}
